package com.bloveoacom.observer;

import com.bloveoacom.exception.ApiException;
import com.bloveoacom.mvp.IListView;

import io.reactivex.subscribers.DisposableSubscriber;

import static com.bloveoacom.http.HttpMethods.PAGE_START_SIZE;

/**
 * author: yjl
 * e-mail: 1271901673@qq.com
 * time  : 2018/9/11
 * desc  :
 */
public class BaseListSubscriber2<T> extends DisposableSubscriber<T> {
    private IListView mIView;
    private boolean isRefresh;
    private int pageIndex;

    public BaseListSubscriber2(IListView mIView, boolean isRefresh, int pageIndex) {
        this.mIView = mIView;
        this.isRefresh = isRefresh;
        this.pageIndex = pageIndex;
    }
    @Override
    public void onNext(T o) {

    }
    @Override
    public void onError(Throwable t) {
        if (pageIndex == PAGE_START_SIZE) {
            if(t instanceof ApiException){
                mIView.showErrorException(((ApiException) t));
            }else{
                mIView.showErrorException(ApiException.handleException(t));
            }
            if (isRefresh) {
                mIView.finishRefresh();
            } else {
                mIView.hideLoadingDialog();
            }
        } else {
            mIView.loadMoreFail();
        }
    }
    @Override
    public void onComplete() {
        if (pageIndex == PAGE_START_SIZE) {
            mIView.hideLoadingDialog();
        }
        mIView.finishRefresh();
    }

    @Override
    protected void onStart() {
        super.onStart();
        if (isRefresh) {
            mIView.showRefreshLoading();
        } else {
            if (pageIndex == PAGE_START_SIZE) {
                mIView.showLoadingDialog("");
            }
        }
    }
}
